package com.lingnan.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.club.pojo.User;
import com.lingnan.club.service.UserService;
import com.lingnan.club.utils.Result;
import com.lingnan.club.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘宇浩
 * @since 2022-04-04
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    /**
     * 查询在线时间排行榜
     * @return
     * 测试通过
     */
    @PostMapping("/orderOnlineTime")
    public Result orderOnlineTime(){
        List<User> list = userService.orderOnlineTime();
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查询在线时间排行榜成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询在线时间排行榜失败！");
    }

    /**
     * 重置学期按钮
     * @return
     * 测试通过
     */
    @PostMapping("/resetTerm")
    public Result resetTerm(){
        UpdateWrapper<User> wrapper = new UpdateWrapper<User>();
        wrapper.set("term_time",0);
        boolean flag = userService.update(wrapper);
        if (flag){
            return ResultGenerator.genSuccessResult(200,"重置学期在线时间成功！");
        }
        return ResultGenerator.genErrorResult(201,"重置学期在线时间失败！");
    }

    /**
     * 查看上一次签到活动未签到成功用户
     * @return
     */
    @PostMapping("findFailSignRecord")
    public Result findFailSignRecord(){
        List<User> list = userService.findSignRecord();
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查看上一次签到活动未签到用户成功！");
        }
        return ResultGenerator.genErrorResult(201,"查看上一次签到活动未签到用户失败或者全部签到成功！");
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag = userService.removeById(id);
        if (flag){
            return ResultGenerator.genSuccessResult(200,"删除用户成功！");
        }
        return ResultGenerator.genErrorResult(201,"删除用户失败！");
    }

    /**
     * 新增或者修改用户
     * @param user
     * @return
     */
    @PostMapping
    public Result save(@RequestBody User user){
        boolean flag = userService.saveOrUpdate(user);
        if (flag){
            return ResultGenerator.genSuccessResult(200,"新增或修改用户成功！");
        }
        return ResultGenerator.genErrorResult(201,"新增或修改用户失败！");
    }

    /**
     * 分页查询所有用户
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     * 测试通过
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
        if (!ObjectUtils.isEmpty(page)) {
            return ResultGenerator.genSuccessResult(200,page,"分页查询所有用户成功！");
        }
        return ResultGenerator.genErrorResult(201,"分页查询所有用户失败！");
    }

    /**
     * 查询当前在线用户
     * @return
     * 测试通过
     */
    @PostMapping("/findOnlineUser")
    public Result findOnlineUser(){
        List<User> list = userService.findOnlineUser();
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查询当前在线用户成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询当前在线用户失败！");
    }
}
